package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.qo.EmplooyeeQueryObject;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RequestMapping("employee")
@Controller
public class EmployeeController {

	@Autowired
	private IEmployeeService employeeService;

	@Autowired
	private IDepartmentService departmentService;

	@Autowired
	private IRoleService roleService;

	@RequestMapping("list")
	public String list(Model model, @ModelAttribute("qo")EmplooyeeQueryObject qo){
		PageInfo<Employee> pageInfo = employeeService.query(qo);
		model.addAttribute("pageInfo",pageInfo);
		List<Department> list = departmentService.list();
		model.addAttribute("list",list);
		return "employee/list";
	}

	@RequestMapping("input")
	public String input(Long id,Model model){
		List<Department> list = departmentService.list();
		model.addAttribute("list",list);
		List<Role> roles = roleService.list();
		model.addAttribute("roles",roles);
		if (id!=null) {
			Employee employee = employeeService.select(id);
			model.addAttribute("employee", employee);
		}
		return "employee/input";
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public JsonResult delete(Long id){
		try {
			if (id!=null) {
				employeeService.delete(id);
			}
		return new JsonResult(true,"删除成功")	;
		}catch (Exception e){
			return new JsonResult(true,"删除失败")	;
		}
	}
	@RequestMapping("saveOrUpdate")
	@ResponseBody
	public JsonResult saveOrUpdate(Employee employee, Long[] roleIds){
		//System.out.println(1);
		//`System.out.println(Arrays.asList(roleIds));
		try {
			if (employee.getId()!=null) {
				employeeService.update(employee,roleIds);
			}else {
				employeeService.insert(employee,roleIds);
			}
		return new JsonResult(true,"新增或修改成功");
		}catch (Exception e){
			e.printStackTrace();
			return new JsonResult(false,"新增或修改失败");
		}
	}
	@RequestMapping("batchDelete")
	@ResponseBody
	public JsonResult batchDelete(Long[] ids){
		//System.out.println(1);
		//`System.out.println(Arrays.asList(roleIds));
		try {
			if (ids!=null && ids.length>0) {
				employeeService.batchDelete(ids);
			}
		return new JsonResult(true,"删除成功");
		}catch (Exception e){
			e.printStackTrace();
			return new JsonResult(false,"删除失败");
		}
	}

	@RequestMapping("exportXls")
	public void exportXls(HttpServletResponse response){
		//设置响应头,告诉浏览器以下载方式打开
		response.setHeader("Content-disposition", "attachment;filename=employee.xls");
		//根据数据库数据动态创建workbook
		Workbook workbook = employeeService.exportXls();
		//把文件响应回给浏览器
		try {
			workbook.write(response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	//返回String重定向到list
	@RequestMapping("importXls")
	public String importXls(MultipartFile file){//参数名跟模态框file类型的name属性的值一样
		//根据上传文件的输入流创建workbook
		try {
			Workbook wb = WorkbookFactory.create(file.getInputStream());
			employeeService.importXls(wb);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "redirect:/employee/list";
	}

}
